package com.itheima.reggie.exception;

import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * Created by IntelliJ IDEA.
 * User: 许志民
 * Date: 2021-11-22
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {

    //声明SQLIntegrityConstraintViolationException类型的异常处理方式
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R ex1(SQLIntegrityConstraintViolationException ex1){
        ex1.printStackTrace();
        return R.error("用户已存在");
    }
    @ExceptionHandler(CustomException.class)
    public R ex2(CustomException ex2){
        return R.error(ex2.getMessage());
    }
//    @ExceptionHandler(IOException.class)
    public R exIo(IOException ex){
        return R.error("文件上传异常,请稍后重试");
    }
}
